<?php

namespace app\common\model;

use think\Db;
/**
 * [AuthRuleSon  子菜单表]
 * @author [C。]
 * @return html
 */
class AuthRuleSon extends Base
{
    protected $name = 'auth_rule_son';

    /**
     * [getAllMenu  获取全部菜单]
     * @author [C。]
     * @return array
     */
    public function getAllMenu()
    {
        return $this->order('sort asc')->select()->toArray();
    }
    /**
     * [getMenu  根据节点数据获取对应的菜单]
     * @author [C。]
     * @param string $nodeStr
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNode($id,$pid = 0)
    {
        $role = new UserTypeSon();
        $rule = $role->getRuleById($id);
        $result = Db::name('auth_rule_son')->field('id,title,pid')->where('pid',0)->select();

        if(!empty($rule)){
            $rule = explode(',', $rule);
        }

        foreach($result as $key=>$vo){
            if(!empty($rule) && in_array($vo['id'], $rule))
                $result[$key]['checked'] = true;
            $result2 = Db::name('auth_rule_son')->field('id,title,pid')->where('pid',$vo['id'])->select();
            $result[$key]['lv'] = $result2;
            if($result2){
                foreach($result2 as $k=>$v){
                    if(!empty($rule) && in_array($v['id'], $rule))
                        $result[$key]['lv'][$k]['checked'] = true;
                    $result3 = Db::name('auth_rule_son')->field('id,title,pid')->where('pid',$v['id'])->select();
                    $result[$key]['lv'][$k]['lv'] = $result3;
                    if($result3) {
                        foreach ($result3 as $k1 => $v1) {
                            if(!empty($rule) && in_array($v1['id'], $rule))
                                $result[$key]['lv'][$k]['lv'][$k1]['checked'] = true;
                        }
                    }
                }
            }
        }
        return $result;
    }
}